﻿




	using System.Collections.Generic;
    using System;
	using System.Linq;    
    using System.Data;        
	using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
using DeklaritModel;

namespace WebSilverlight.Web
{
	[EnableClientAccess()]    
    public partial class InvoiceDomainService : DomainService
    { 
		#region Properties						
		protected DeklaritModel.InvoiceDataSet ContextDataSet;
		// Setear Property (global o anivel de entidad o ambas?)
		const int PageSize = 6;
        
        public virtual IInvoiceDataAdapter DataAdapter 
        {
            get 
            {
				if(DataAdapterFactory.Provider == null) //return null;
					DataAdapterFactory.Provider = new SimpleDataAdapterFactory();

                return DataAdapterFactory.GetInvoiceDataAdapter(); 
            }
        }
		
		#endregion
        
        private void FillInvoiceDataSet(DeklaritModel.InvoiceDataSet dsInvoiceDataSet1, int page, Int16? InvoiceId , Int16? fkClienteId)
        {   
            if (InvoiceId.HasValue )
            {   
				DataAdapter.Fill(dsInvoiceDataSet1, InvoiceId.Value);                            
            }
            else
            {
			    if(fkClienteId.HasValue)
				{
					if (page == -1)
						DataAdapter.FillByClienteId(dsInvoiceDataSet1, fkClienteId.Value);
                    else DataAdapter.FillPageByClienteId(dsInvoiceDataSet1, fkClienteId.Value, (page - 1) * PageSize, PageSize);
				}
				else				
					if (page == -1)
						DataAdapter.Fill(dsInvoiceDataSet1);
					else DataAdapter.FillPage(dsInvoiceDataSet1, (page-1) * PageSize, PageSize);				
            }      
        } 
        private IQueryable<InvoiceWrapper> GetPage(int pageNumber, Int16? fkClienteId)
        {
            ContextDataSet = new DeklaritModel.InvoiceDataSet();
            FillInvoiceDataSet(ContextDataSet, pageNumber, null , fkClienteId);

            EnumerableRowCollection<DataRow> dr = ContextDataSet.Invoice.AsEnumerable();
            
			IEnumerable<InvoiceWrapper> query = from row in            
                            dr
                        select new InvoiceWrapper
                        {
							InvoiceId = row.Field<Int16>("InvoiceId"),
							InvoiceDescription = row.Field<string>("InvoiceDescription"),
							ClienteId = row.Field<Int16>("ClienteId"),
							ClienteName = row.Field<string>("ClienteName"),
							InvoiceLineLastIndex = row.Field<Int16>("InvoiceLineLastIndex"),
                        };            
            IQueryable<InvoiceWrapper> iq = query.AsQueryable();            
            return iq;            
        } 
                       
        public IQueryable<InvoiceWrapper> GetInvoicePage(int pageNumber)
        {
            return GetPage(pageNumber , null);
        }       
		public IQueryable<InvoiceWrapper> GetInvoice()
        {
            return GetPage(-1 , null);
        }
        public IQueryable<InvoiceWrapper> GetInvoiceByClienteId(Int16 ClienteId)
        {            
            return GetPage(-1, ClienteId);
        }  
        private void GetInvoice(Int16? InvoiceId)
        {
			ContextDataSet = new DeklaritModel.InvoiceDataSet();
            FillInvoiceDataSet(ContextDataSet, -1, InvoiceId.Value, null);
        }
		public void UpdateInvoice(InvoiceWrapper current)        
        {			
			GetInvoice(current.InvoiceId);

            DataRow updateRow = null;
            foreach (DataRow row in ContextDataSet.Invoice.Rows)
            {
                updateRow = row;                
                break;
            }

            //var Changes = this.ChangeSet.GetOriginal(current);
			//if (Changes.InvoiceId != current.InvoiceId)
                updateRow.SetField("InvoiceId", current.InvoiceId);							
			//if (Changes.InvoiceDescription != current.InvoiceDescription)
                updateRow.SetField("InvoiceDescription", current.InvoiceDescription);							
			//if (Changes.ClienteId != current.ClienteId)
                updateRow.SetField("ClienteId", current.ClienteId);							
			//if (Changes.ClienteName != current.ClienteName)
                updateRow.SetField("ClienteName", current.ClienteName);							
			//if (Changes.InvoiceLineLastIndex != current.InvoiceLineLastIndex)
                updateRow.SetField("InvoiceLineLastIndex", current.InvoiceLineLastIndex);							
			DataAdapter.Update(ContextDataSet);    
        }        
        public void InsertInvoice(InvoiceWrapper newObj)        
        {			
			DeklaritModel.InvoiceDataSet  dsInvoiceDataSet1 = new DeklaritModel.InvoiceDataSet();            
			InvoiceDataSet.InvoiceRow row = dsInvoiceDataSet1.Invoice.NewInvoiceRow();
			row.InvoiceId  = newObj.InvoiceId;
			row.InvoiceDescription  = newObj.InvoiceDescription;
			row.ClienteId  = newObj.ClienteId;
			row.ClienteName  = newObj.ClienteName;
			row.InvoiceLineLastIndex  = newObj.InvoiceLineLastIndex;
            dsInvoiceDataSet1.Invoice.AddInvoiceRow(row);
            DataAdapter.Update(dsInvoiceDataSet1);             
        }
        public void DeleteInvoice(InvoiceWrapper delObj)        
        {			
			DeklaritModel.InvoiceDataSet  dsInvoiceDataSet1 = new DeklaritModel.InvoiceDataSet();            
			InvoiceDataSet.InvoiceRow row = dsInvoiceDataSet1.Invoice.NewInvoiceRow();
			row.InvoiceId  = delObj.InvoiceId;
			row.InvoiceDescription  = delObj.InvoiceDescription;
			row.ClienteId  = delObj.ClienteId;
			row.ClienteName  = delObj.ClienteName;
			row.InvoiceLineLastIndex  = delObj.InvoiceLineLastIndex;
            dsInvoiceDataSet1.Invoice.RemoveInvoiceRow(row);
            DataAdapter.Update(dsInvoiceDataSet1);             
        }
        [Invoke]
        public int ItemCount()
        {            
            return DataAdapter.GetRecordCount();
        }
	}
}